package com.skycode.dao;

import com.skycode.bean.NewsBean;
import com.skycode.util.DBConnection;
import com.skycode.util.Time;

import java.util.Date;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsDao {
    DBConnection DBConn = new DBConnection();
    public List<NewsBean> getList(int page){
        Connection conn = DBConn.getConn();
        String sql = "select * from news limit "+(page-1)*10+",10 ";
        List<NewsBean> list = new ArrayList<NewsBean>();
        list = null;
        try{
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()){
                NewsBean newsBean = new NewsBean();
                newsBean.setNewsID(rs.getInt("NewsID"));
                newsBean.setNewsTitle(rs.getString("NewsTitle"));
                newsBean.setNewsContent(rs.getString("NewsContent"));
                newsBean.setNewsTime(Integer.parseInt(rs.getString("NewsTime")));
                newsBean.setAdminName(rs.getString("AdminName"));
                list.add(newsBean);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     *
     * @param news
     * @return
     */
    public int add(NewsBean news) {
        Connection conn = DBConn.getConn();
        String sql = "update news set NewsTitle=?,NewsConten=?,NewsTime=?,NewsName=? where NewsID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, news.getNewsTitle());
            pmst.setString(2, news.getNewsContent());
            pmst.setString(3, Time.getDate());
            pmst.setString(4, news.getAdminName());
            pmst.setInt(5, news.getNewsID());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     *
     * @return
     */
    public int count(){
        Connection conn = DBConn.getConn();
        try {
            String sql = "select count(*) form news";
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            if(rs.next()){
                return rs.getInt(1);//count查询返回一行数据 第一行第一列就是总条数
            }
        } catch (SQLException e ){
            e.printStackTrace();
        }
        return 0;
    }
    public int update(NewsBean news) {
        Connection conn = DBConn.getConn();
        String sql = "update news set NewsTitle=?,NewsConten=?,NewsTime=?,NewsName=? where NewsID=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, news.getNewsTitle());
            pmst.setString(2, news.getNewsContent());
            pmst.setString(3, (new Date()).toString());
            pmst.setString(4, news.getAdminName());
            pmst.setInt(5, news.getNewsID());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int delete(int newsID){
        Connection conn = DBConn.getConn();
        try{
            String aql = "delete from news where NesID=?";
            String sql = null;
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1,newsID);
            return pmst.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return  0;
    }
    public NewsBean getNewsByID(int id) {
        String sql ="select * from news where NewsID=?";
        NewsBean newsBean = new NewsBean();
        Connection conn = DBConn.getConn();
        PreparedStatement pmst= conn.prepareStatement(sql);
        pmst.setInt(1,id);
        ResultSet rs = pmst.executeQuery();
        if (rs.next()){
            newsBean.getNewsID(rs.getInt(1));
            newsBean.getNewsTitle(rs.getString(2));
            newsBean.getNewsContent(rs.getString(3));
            newsBean.getNewsTime(rs.getString(4));
            newsBean.setAdminName(rs.getString(5));
            return newsBean;
        }
    }catch (SQLException e){
        e.printStackTrace();
    }
    return null;
}


